package cn.bugstack.springframework.beans.factory.support;

import cn.bugstack.springframework.beans.BeanException;
import cn.bugstack.springframework.beans.factory.BeanFactory;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import cn.bugstack.springframework.beans.factory.config.DefaultSingletenBeanRegistry;

public abstract class AbstractBeanFactory extends DefaultSingletenBeanRegistry implements BeanFactory {


    @Override
    public Object getBean(String name) throws BeanException {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name,beanDefinition);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeanException;

    protected abstract Object createBean(String beanName,BeanDefinition beanDefinition) throws BeanException;

}
